1   /*
2    * Copyright (C) 2011 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13  */
14  package com.google.common.collect;
15  
16  import com.google.common.annotations.GwtCompatible;
17  import com.google.common.annotations.GwtIncompatible;
18  
19  import java.io.Serializable;
20  import java.util.NoSuchElementException;
21  import java.util.Set;
22  
23  import javax.annotation.Nullable;
24  
25  /**
26   * An empty contiguous set.
27   *
28   * @author Gregory Kick
29   */
30  @GwtCompatible(emulated = true)
31  @SuppressWarnings("unchecked") // allow ungenerified Comparable types
32  final class EmptyContiguousSet<C extends Comparable> extends ContiguousSet<C> {
33    EmptyContiguousSet(DiscreteDomain<C> domain) {
34      super(domain);
35    }
36  
37    @Override public C first() {
38      throw new NoSuchElementException();
39    }
40  
41    @Override public C last() {
42      throw new NoSuchElementException();
43    }
44  
45    @Override public int size() {
46      return 0;
47    }
48  
49    @Override public ContiguousSet<C> intersection(ContiguousSet<C> other) {
50      return this;
51    }
52  
53    @Override public Range<C> range() {
54      throw new NoSuchElementException();
55    }
56  
57    @Override public Range<C> range(BoundType lowerBoundType, BoundType upperBoundType) {
58      throw new NoSuchElementException();
59    }
60  
61    @Override ContiguousSet<C> headSetImpl(C toElement, boolean inclusive) {
62      return this;
63    }
64  
65    @Override ContiguousSet<C> subSetImpl(
66        C fromElement, boolean fromInclusive, C toElement, boolean toInclusive) {
67      return this;
68    }
69  
70    @Override ContiguousSet<C> tailSetImpl(C fromElement, boolean fromInclusive) {
71      return this;
72    }
73  
74    @GwtIncompatible("not used by GWT emulation")
75    @Override int indexOf(Object target) {
76      return -1;
77    }
78  
79    @Override public UnmodifiableIterator<C> iterator() {
80      return Iterators.emptyIterator();
81    }
82  
83    @GwtIncompatible("NavigableSet")
84    @Override public UnmodifiableIterator<C> descendingIterator() {
85      return Iterators.emptyIterator();
86    }
87  
88    @Override boolean isPartialView() {
89      return false;
90    }
91  
92    @Override public boolean isEmpty() {
93      return true;
94    }
95  
96    @Override public ImmutableList<C> asList() {
97      return ImmutableList.of();
98    }
99  
100   @Override public String toString() {
101     return "[]";
102   }
103 
104   @Override public boolean equals(@Nullable Object object) {
105     if (object instanceof Set) {
106       Set<?> that = (Set<?>) object;
107       return that.isEmpty();
108     }
109     return false;
110   }
111 
112   @Override public int hashCode() {
113     return 0;
114   }
115 
116   @GwtIncompatible("serialization")
117   private static final class SerializedForm<C extends Comparable> implements Serializable {
118     private final DiscreteDomain<C> domain;
119 
120     private SerializedForm(DiscreteDomain<C> domain) {
121       this.domain = domain;
122     }
123 
124     private Object readResolve() {
125       return new EmptyContiguousSet<C>(domain);
126     }
127 
128     private static final long serialVersionUID = 0;
129   }
130 
131   @GwtIncompatible("serialization")
132   @Override
133   Object writeReplace() {
134     return new SerializedForm<C>(domain);
135   }
136 
137   @GwtIncompatible("NavigableSet")
138   ImmutableSortedSet<C> createDescendingSet() {
139     return new EmptyImmutableSortedSet<C>(Ordering.natural().reverse());
140   }
141 }